﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Adds new node as a child node of the currently selected node.  
            var newNode = new TreeNode("字节工具");
            newNode.Nodes.Add("sig 转换" );
            newNode.Nodes.Add(@"\x转码");
            newNode.Nodes.Add(@"文件转代码");
            treeView1.Nodes.Add(newNode);


            newNode = new TreeNode("计算工具");
            newNode.Nodes.Add("算式计算" );
            treeView1.Nodes.Add(newNode);

            treeView1.ExpandAll();
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)  //单击鼠标左键才响应
            {
                if (e.Node.Level == 1)                               //判断子节点才响应
                {
                    chooseUserControl(e.Node.Text);                   //文件框中显示鼠标点击的节点名称
                }
            }
        }


        private void showControl(Type type)
        {
            if (panelRight.Controls.Count == 1)
            {
                var ctrl = panelRight.Controls[0];
                if (ctrl != null && ctrl.GetType() == type)
                    return;
            }

            panelRight.Controls.Clear();
            var UserControl1tc = System.Activator.CreateInstance(type) as UserControl;
            this.panelRight.Controls.Add(UserControl1tc);

        }

        private void chooseUserControl(string text)
        {
            if (string.IsNullOrEmpty(text))
                return;

            switch (text)
            {
                case "sig 转换":
                    showControl(typeof(Sig2Code));
                    break;

                case "文件转代码":
                    showControl(typeof(File2Code));
                    break;


                case "算式计算":
                    showControl(typeof(Calc));
                    break;

            }

        }


    }
}
